package it.polimi.entities.web;

import java.util.ArrayList;
import java.util.List;

import it.polimi.entitiesDTO.PacketDTO;
import it.polimi.entitiesManagers.AttractionMgr;
import it.polimi.entitiesManagers.HotelMgr;
import it.polimi.entitiesManagers.PacketMgr;
import it.polimi.entitiesManagers.TransportMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "PacketBean")
@SessionScoped
public class PacketBean {
	
	@EJB
	private HotelMgr hmgr;
	
	@EJB
	private TransportMgr tmgr;
	
	@EJB
	private PacketMgr pmgr;
	
	@EJB
	private AttractionMgr amgr;
	
	private PacketDTO packetDTO;
	
	private List<String> hotels = new ArrayList<String>();
	private List<String> arrivalFlights = new ArrayList<String>();
	private List<String> departureFlights = new ArrayList<String>();
	private List<String> attractions = new ArrayList<String>();
	private List<Integer> attractionsId= new ArrayList<Integer>();
	private int temp=0;
	private int voloAndata;
	private int voloRitorno;
	private int removalID;
	private List<Integer> allpackets;
	
	
	private String city;
	
	/*
	 * -----------------------------------------
	 * ----------CLASS METHODS------------------
	 * -----------------------------------------
	 */

	public PacketBean(){
		this.packetDTO = new PacketDTO();
		clearAll();
	}
	
	public String remove(){
		this.allpackets = pmgr.getAllPacketId();
		if(this.allpackets.contains(this.removalID))
			{pmgr.remove(this.removalID);
			return "/admin/index";
			}
		else return "/admin/errorPage";
	}
	public void search(){
		hotels = pmgr.getHotelByCity(city);
		departureFlights=tmgr.findFlightByDeparture(this.city);
		arrivalFlights=tmgr.findFlightByArrival(this.city);
		setAttractions(amgr.getAttractionByCity(this.city));
	}
	
	public void addAttraction(){
		if(!attractionsId.contains(temp))
		attractionsId.add((Integer)this.temp);
		this.temp=0;
	}
	
	public String register() {
		pmgr.save(this.packetDTO);
		return "/admin/index";
	}
	
	public String registerFlight(){
		pmgr.registerFlight(voloAndata, voloRitorno);
		return "/admin/index";
	}
	
	public String registerAttraction(){
		pmgr.registerAttraction(attractionsId);
		return "/admin/index";
	}
	
	private void clearAll(){
		this.hotels = new ArrayList<String>();
		this.arrivalFlights = new ArrayList<String>();
		this.departureFlights = new ArrayList<String>();
		this.attractions = new ArrayList<String>();
		this.attractionsId= new ArrayList<Integer>();
		this.temp=0;
		this.voloAndata =0;
		this.voloRitorno =0;
		this.removalID=0;
		this.allpackets = new ArrayList<Integer>();
	}
	
	/*
	 * -----------------------------------------
	 * ----------SETTERS AND GETTERS------------
	 * -----------------------------------------
	 */

	public PacketDTO getPacketDTO() {
		return packetDTO;
	}

	public void setPacketDTO(PacketDTO packetDTO) {
		this.packetDTO = packetDTO;
	}
	
	

	public List<String> getHotels() {
		return hotels;
	}

	public void setHotels(List<String> hotels) {
		this.hotels = hotels;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public List<String> getArrivalFlights() {
		return arrivalFlights;
	}

	public void setArrivalFlights(List<String> arrivalFlights) {
		this.arrivalFlights = arrivalFlights;
	}

	public List<String> getDepartureFlights() {
		return departureFlights;
	}

	public void setDepartureFlights(List<String> departureFlights) {
		this.departureFlights = departureFlights;
	}

	public List<String> getAttractions() {
		return attractions;
	}

	public void setAttractions(List<String> attractions) {
		this.attractions = attractions;
	}

	public int getVoloAndata() {
		return voloAndata;
	}

	public void setVoloAndata(int voloAndata) {
		this.voloAndata = voloAndata;
	}

	public int getVoloRitorno() {
		return voloRitorno;
	}

	public void setVoloRitorno(int voloRitorno) {
		this.voloRitorno = voloRitorno;
	}

	public List<Integer> getAttractionsId() {
		return attractionsId;
	}

	public void setAttractionsId(List<Integer> attractionsId) {
		this.attractionsId = attractionsId;
	}

	public int getTemp() {
		return temp;
	}

	public void setTemp(int temp) {
		this.temp = temp;
	}

	public int getRemovalID() {
		return removalID;
	}

	public void setRemovalID(int removalID) {
		this.removalID = removalID;
	}

	public List<Integer> getAllpackets() {
		return allpackets;
	}

	public void setAllpackets(List<Integer> allpackets) {
		this.allpackets = allpackets;
	}
	
}
